<template>
  <div class="login">
    <div class="login-box">
      <img class="loging-img" src="@/assets/common/login-logo.png" alt="">
      <el-form ref="from" :model="from" :rules="rules">
        <el-form-item class="inp" prop="mobile">
          <el-input
            v-model="from.mobile"
            prefix-icon="el-icon-user-solid"
            class="inp"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="from.password"
            prefix-icon="el-icon-lock"
            show-password
            class="inp"
            @keyup.native="submit"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="btn-login"
            :loading="loading"
            @click="submit"
          >登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      from: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确手机号', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确手机号'))
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '长度在 3 到 5 个字符',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.from.validate(async(result) => {
        if (result) {
          try {
            this.loading = true
            await this.$store.dispatch('user/getToken', this.from)
            this.loading = false
            const redirect = this.$route.query.redirect || '/'
            this.$router.push(redirect)
          } catch {
            this.loading = false
          }
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.doubanio.com%2Fview%2Fgroup_topic%2Fl%2Fpublic%2Fp379495383.jpg&refer=http%3A%2F%2Fimg2.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642499485&t=1455524f79a159dcf0ada61390bd0373');
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-box {
    width: 500px;
    height: 500px;
    .inp {
      margin: 10px 0;
    }
    .loging-img {
      margin-bottom: 20px;

      margin-left: 32px;
    }
    .btn-login {
      width: 500px;
      height: 50px;
      background-color: #1f69fe;
      color: white;
      border: none;
    }
  }
}
</style>
